package com.wy.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

/**
 * @Author:王渝
 * @Date: 2022/11/2  9:58
 * @Version 1.0
 */
//发送邮箱
@Component
@Slf4j
public class EmailSender {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Async("taskScheduler")
    public void send(String email,String message)  {
        try {
            log.info("发送邮箱.....");
            MimeMessage mimMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimMessage, true);
            mimeMessageHelper.setSubject("欢迎使用我的线上拍卖系统");
            mimeMessageHelper.setText( message,true);
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setFrom(from);
            mailSender.send(mimMessage);
            log.info("发送完毕...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
